import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'
import Nest2 from '@/layout/Nest2.vue'
import NProgress from 'nprogress'
import Utils from '@/utils/utils'
import store from '@/store'
// import { routes } from '../store/modules/menu';
Vue.use(VueRouter)
//动态路由
let dynamicRoutes = []

const routes = [
  {
    path: '/',
    name: 'Layout',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/HomeView.vue')
      },
      // {
      //   path: 'tableLayout',
      //   name: 'tableLayout',
      //   component: () => import('@/views/TableLayout/index.vue'),
      // },
    ]
  },
  // {
  //   path: '/',
  //   component: Layout,
  //   children: [
  //     {
  //       path:"",
  //       component:Nest2,
  //       children:[
  //         {
  //           path:"home",
  //           name:"home",
  //           component:()=>import('@/views/HomeView.vue')
  //         }
  //       ]
  //     }
  //   ]
  // },
  // {
  //   path: '/',
  //   component: Layout,
  //   children: [
  //     {
  //       path:"",
  //       component:Nest2,
  //       children:[
  //         {
  //           path:"tableLayout",
  //           name:"tableLayout",
  //           component:()=>import('@/views/TableLayout/index.vue')
  //         }
  //       ]
  //     }
  //   ]
  // },
  // {
  //   path: '/',
  //   component: Layout,
  //   children: [
  //     {
  //       path:"",
  //       component:Nest2,
  //       children:[
  //         {
  //           path:"test",
  //           name:"test",
  //           component:()=>import('@/views/Test/index.vue')
  //         },
  //         {
  //           path:"child",
  //           name:"child",
  //           component:()=>import('@/views/Test/Child/index.vue')
  //         }
  //       ]
  //     }
  //   ]
  // },
  // {
  //   path: '/',
  //   name: 'Layout',
  //   // redirect: '/home',
  //   component: Layout,
  //   children: [
  //     // {
  //     //   path: 'home',
  //     //   name: 'home',
  //     //   component: () => import('@/views/HomeView.vue')
  //     // },
  //     {
  //       path: 'tableLayout',
  //       name: 'tableLayout',
  //       component: () => import('@/views/TableLayout/index.vue'),
  //     },
  //   ]
  // },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  }
]

function getRoutes() {
  return new Promise(resolve => {
    setTimeout(() => {
      // console.log(store.state.menu.menus);
      resolve(store.state.menu.routes)
    }, 100)
  })
}
// console.log('routes',routes);
let router = new VueRouter({
  routes
})



router.onReady(()=>{
  getRoutes().then(res => {
    console.log('getRoutes', res)
    // dynamicRoutes = res
    //处理路由导航守卫
    // console.log(routes)
    // router = new VueRouter({
    //   routes
    // })
    //使用addRoutes方法添加动态路由
    // res.forEach((item)=>{
    //   router.addRoute(item)
    // })
  })
})
//重写push方法，跳转相同路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)

  return originalPush.call(this, location).catch(err => err)
}

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  // const token = utils.getStorage('userInfo')
  let UserName = Utils.getStorage('qt-v0-Hyper-Hyper-Accout_beihang')
  let Password = Utils.getStorage('qt-v0-Hyper-Hyper-Password_beihang')
  if (to.path === '/login') {
    next()
  }
  //用户信息存在
  if (!UserName || !Password) {
    next({ name: 'login' })
  } else {
    next()
    NProgress.done()
  }
})

export default router
